package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/1/3 19:36
 * @description
 */
public class Code682 {
    //["5","2","C","D","+"]
    //["-21","-66","39","+","+"]
    public int calPoints(String[] ops) {
        List<Integer> result = new ArrayList<>();
        for (int i = 0, j = 0; i < ops.length; i++) {
            if (!"CD+".contains(ops[i])) {
                result.add(j, Integer.parseInt(ops[i]));
            }
            if ("C".equals(ops[i])) {
                result.remove(--j);
                continue;
            }
            if ("D".equals(ops[i])) {
                result.add(j, result.get(j - 1) * 2);
            }
            if ("+".equals(ops[i])) {
                result.add(j, result.get(j - 1) + result.get(j - 2));
            }
            j++;
        }
        return result.stream().mapToInt(x -> x).sum();
    }
}
